package utils

import (
	"fmt"
	"gitee.com/sansaniot/ssiot-core/facade/env"
	"gitee.com/sansaniot/ssiot-core/logger"
	"ssadmin/common/constant"
	"strings"
)

type CacheUtil struct {
}

// SetUserProfile 设置登录用户缓存
// @userId 登录用户ID
// @profile 用户profile(user:userInfo,deptIds:["1","2","3"])
func SetUserProfile(userName string, profile map[string]interface{}) {
	key := fmt.Sprintf("login_user:%s", userName)

	if err := env.Cache.HashSet(key, profile); err != nil {
		logger.Warn(err.Error())
		return
	}
}

// GetUserDeptIds 获取登录用户机构
// @userId 登录用户ID
// @return 用户信息(profile.deptIds=>["1","2","3"])
func GetUserDeptIds(username string) []string {
	key := fmt.Sprintf("login_user:%s", username)
	deptIds, _ := env.Cache.HashGet(key, constant.CacheKeyUserDeptIds)
	//deptIds := global.CacheOperator.HgetField(key, global.CacheKeyUserDeptIds)
	return strings.Split(deptIds, ",")
}

// GetUserRoleIds 获取登录用户角色
func GetUserRoleIds(userId string) []string {
	key := fmt.Sprintf("login_user:%s", userId)
	roleIds, _ := env.Cache.HashGet(key, constant.CacheKeyUserId)
	return strings.Split(roleIds, ",")
}

//type Profile map[string]interface{}
//
//func (p Profile) MarshalBinary() ([]byte, error) {
//	return json.Marshal(p)
//}
//
//func (p Profile) UnmarshalBinary(data []byte) error {
//	return json.Unmarshal(data, &p)
//}
